using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace MinesweeperGame
{
    public partial class LeaderboardForm : Form
    {
        private Leaderboard leaderboard;
        private TabControl tabControl;
        private Button closeButton;
        private Button resetButton;

        public LeaderboardForm(Leaderboard leaderboard)
        {
            this.leaderboard = leaderboard;
            InitializeComponent();
            LoadLeaderboards();
        }

        private void InitializeComponent()
        {
            this.Text = "排行榜";
            this.Size = new Size(600, 500);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            // 创建TabControl
            tabControl = new TabControl();
            tabControl.Dock = DockStyle.Top;
            tabControl.Size = new Size(580, 400);

            // 为每个难度创建TabPage
            foreach (Difficulty difficulty in Enum.GetValues(typeof(Difficulty)))
            {
                TabPage tabPage = new TabPage(Leaderboard.GetDifficultyDisplayName(difficulty));
                tabPage.Tag = difficulty;
                
                // 创建ListView显示排行榜
                ListView listView = new ListView();
                listView.Dock = DockStyle.Fill;
                listView.View = View.Details;
                listView.FullRowSelect = true;
                listView.GridLines = true;
                listView.Font = new Font("Microsoft YaHei", 9);

                // 添加列
                listView.Columns.Add("排名", 60);
                listView.Columns.Add("玩家姓名", 120);
                listView.Columns.Add("时间", 80);
                listView.Columns.Add("日期", 150);

                tabPage.Controls.Add(listView);
                tabControl.TabPages.Add(tabPage);
            }

            // 创建按钮面板
            Panel buttonPanel = new Panel();
            buttonPanel.Dock = DockStyle.Bottom;
            buttonPanel.Height = 50;
            buttonPanel.Padding = new Padding(10);

            // 关闭按钮
            closeButton = new Button();
            closeButton.Text = "关闭";
            closeButton.Size = new Size(80, 30);
            closeButton.Location = new Point(buttonPanel.Width - 100, 10);
            closeButton.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            closeButton.Click += CloseButton_Click;

            // 重置按钮
            resetButton = new Button();
            resetButton.Text = "重置排行榜";
            resetButton.Size = new Size(100, 30);
            resetButton.Location = new Point(10, 10);
            resetButton.Click += ResetButton_Click;

            buttonPanel.Controls.Add(closeButton);
            buttonPanel.Controls.Add(resetButton);

            this.Controls.Add(tabControl);
            this.Controls.Add(buttonPanel);
        }

        private void LoadLeaderboards()
        {
            foreach (TabPage tabPage in tabControl.TabPages)
            {
                Difficulty difficulty = (Difficulty)tabPage.Tag;
                ListView listView = (ListView)tabPage.Controls[0];
                
                var entries = leaderboard.GetLeaderboard(difficulty);
                listView.Items.Clear();

                for (int i = 0; i < entries.Count; i++)
                {
                    var entry = entries[i];
                    var item = new ListViewItem((i + 1).ToString());
                    item.SubItems.Add(entry.PlayerName);
                    item.SubItems.Add(Leaderboard.FormatTime(entry.Time));
                    item.SubItems.Add(entry.Date.ToString("yyyy-MM-dd HH:mm"));

                    // 为前三名设置特殊颜色
                    if (i == 0)
                    {
                        item.BackColor = Color.Gold;
                        item.ForeColor = Color.Black;
                    }
                    else if (i == 1)
                    {
                        item.BackColor = Color.Silver;
                        item.ForeColor = Color.Black;
                    }
                    else if (i == 2)
                    {
                        item.BackColor = Color.FromArgb(205, 127, 50); // 铜色
                        item.ForeColor = Color.White;
                    }

                    listView.Items.Add(item);
                }

                // 如果没有记录，显示提示信息
                if (entries.Count == 0)
                {
                    var item = new ListViewItem("暂无记录");
                    item.SubItems.Add("");
                    item.SubItems.Add("");
                    item.SubItems.Add("");
                    listView.Items.Add(item);
                }
            }
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show(
                "确定要重置所有排行榜吗？此操作不可撤销。",
                "重置排行榜",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                leaderboard.ClearAllLeaderboards();
                LoadLeaderboards();
                MessageBox.Show("排行榜已重置。", "重置完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
} 